1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9 using System.Windows.Forms;
10
11 namespace SoftQuanLyNhaHang.Views
12 {
13 public partial class frmKho_MvtMsp : Form
14 {
15 public frmKho_MvtMsp()
16 {
17 InitializeComponent();
18 }
19
20 private string _getMaSanPham;
21 private string _getMaVietTat;
22 private string _getTenSanPham;
23 private double _getSoLuong;
24
25 public frmKho_MvtMsp(string tenSanPham, string maSanPham, string maVietTat, double SLuong) : this()
26 {
27 _getTenSanPham = tenSanPham;
28 _getMaSanPham = maSanPham;
29 _getMaVietTat = maVietTat;
30 _getSoLuong = SLuong;
31 }
32
33 private void HienThiThongTin()
34 {
35 txtTenSanPham.Text = _getTenSanPham;
36 txtMaSanPham.Text = _getMaSanPham;
37 txtMaVietTat.Text = _getMaVietTat;
38 txtSoLuongTrongKho.Text = CauHinhHeThong.Format_SoDeHienThi(_getSoLuong, false);
39 }
40
41 private void frmKho_MvtMsp_Load(object sender, EventArgs e)
42 {
43 this.HienThiThongTin();
44 }
45
46 private void btnThucHien_Click(object sender, EventArgs e)
47 {
48
49 int idKho = 0;
50
51 //hien thi danh sach cac san pham can update
52 DataTable dtKho = new DataTable();
53
54 dtKho = Controllers.KhoCtrl.FillDataSet_getKho_ByMaSanPham_MaVietTat_All(_getMaSanPham, -1, _getMaVietTat).Tables[0];
55
56 if (dtKho.Rows.Count > 0)
57 {
58 double soluongInSertMoi = 0;
59
60 string _MaSanPham = txtMaSanPham.Text.Trim();
61 string _MaVietTat = txtMaVietTat.Text.Trim();
62 string _TenSanPham = txtTenSanPham.Text.Trim();
63
64 if (string.IsNullOrEmpty(txtSoLuongTrongKho.Text.Trim()))
65 {
66 txtSoLuongTrongKho.Focus();
67 return;
68 }
69
70 if (string.IsNullOrEmpty(txtSoLuongThayDoi.Text.Trim()))
71 {
72 txtSoLuongThayDoi.Focus();
73 return;
74 }
75
76 double soLuongCoTrongKho = double.Parse(txtSoLuongTrongKho.Text.Trim());
77 double soLuongcanThayDoi = double.Parse(txtSoLuongThayDoi.Text.Trim());
78 //nếu có 1 bản ghi thì
79 if (dtKho.Rows.Count == 1)
80 {
81 idKho = Int32.Parse( dtKho.Rows[0]["IdKho"].ToString());
82
83 //nếu số lượng update = số lượng có trong kho
84 if (soLuongCoTrongKho == soLuongcanThayDoi)
85 {
86 Controllers.KhoCtrl.UpdateKho_ByMaSanPham_MaVietTat(idKho, _MaSanPham, _MaVietTat, _TenSanPham, soLuongcanThayDoi);
87 }
88 else
89 {
90 //số lượng thay đổi nhỏ hơn số số lượng có trong kho
91 Controllers.KhoCtrl.UpdateKho_ByMaSanPham_MaVietTat(idKho, _MaSanPham, _MaVietTat, _TenSanPham, soLuongcanThayDoi);
92
93 //insert them 1 sản phẩm trong kho cho đủ số lượng cần thay đổi
94 soluongInSertMoi = soLuongCoTrongKho - soLuongcanThayDoi;
95
96 //Thêm một bản ghi vào bảng kho, khác mỗi số lượng của sản phậm
97 Controllers.KhoCtrl.InsertKho_Select_ByIdKho(idKho, _MaSanPham, _MaVietTat, _TenSanPham, soluongInSertMoi);
98
99 }
100 }
101 else
102 {
103 //số lượng cần thay đổi nhỏ hơn số lượng trong kho
104 double tongSoluongsanPhamTrongCsdl = 0;
105
106 foreach (DataRow dr in dtKho.Rows)
107 {
108 tongSoluongsanPhamTrongCsdl += double.Parse(dr["SoLuong"].ToString());
109
110 idKho = Int32.Parse(dr["IdKho"].ToString());
111
112 //giá trị update soluong trong csdl
113 double soluongHienTaiCuaSanPham = double.Parse(dr["SoLuong"].ToString());
114
115 //can thay doi 100
116 //trong csdl 10
117 if (tongSoluongsanPhamTrongCsdl <= soLuongcanThayDoi)
118 {
119 Controllers.KhoCtrl.UpdateKho_ByMaSanPham_MaVietTat(idKho, _MaSanPham, _MaSanPham, _TenSanPham, soluongHienTaiCuaSanPham);
120 }
121 else
122 {
123 double soluongUpdate = (soluongHienTaiCuaSanPham + soLuongcanThayDoi) - tongSoluongsanPhamTrongCsdl;
124 //số lượng thay đổi nhỏ hơn số số lượng có trong kho
125 Controllers.KhoCtrl.UpdateKho_ByMaSanPham_MaVietTat(idKho, _MaSanPham, _MaVietTat, _TenSanPham, soluongUpdate);
126
127 //insert them 1 sản phẩm trong kho cho đủ số lượng cần thay đổi
128 soluongInSertMoi = tongSoluongsanPhamTrongCsdl - soLuongcanThayDoi;
129
130 //Thêm một bản ghi vào bảng kho, khác mỗi số lượng của sản phậm
131 Controllers.KhoCtrl.InsertKho_Select_ByIdKho(idKho, _MaSanPham, _MaVietTat, _TenSanPham, soluongInSertMoi);
132
133 //nhảy khỏi vòng lặp
134 //break;
135 }
136 }
137
138 }
139
140 //số lượng gồm
141 //10
142 //4
143 //1
144
145 //số lượng thay đổi 9 or 12
146
147
148
149 }
150
151
152
153 }
154 }
155 }